;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(planet plai/plai:1:3/lang/reader)
;; The Little Schemer
;; Chapter 6. Shadows

; pre-requisite
(define atom?
  (lambda (x)
    (and (not (pair? x)) (not (null? x)))))

; p.99
(define numeric?
  (lambda (s)
    (cond
      ((null? s) #t)
      ((atom? s) (number? s))
      ((and (numeric? (car s)) (null? (cdr s))) #t)
      (else
       (and (numeric? (car s)) 
            (numeric? (cdr (cdr s))))))))

; p.102
(define value
  (lambda (nexp)
    (cond
      ((atom? nexp) nexp)
      ((eq? (car (cdr nexp)) '+) (+ (value (car nexp)) (value (car (cdr (cdr nexp))))))
      ((eq? (car (cdr nexp)) '*) (* (value (car nexp)) (value (car (cdr (cdr nexp))))))
      ((eq? (car (cdr nexp)) '^) (expt (value (car nexp)) (value (car (cdr (cdr nexp))))))
      (else "erro"))))


;;; tests
(print-only-errors #t)

(test (numeric? '(3)) #t)
(test (numeric? '(3 + (4 * 5))) #t)

(test (value '(3 + (4 * 5))) 23)
(test (value '((3 + 4) * 5)) 35)
(test (value '(2 ^ 10)) 1024)

(include "test-summary.scm")